home *** CD-ROM | disk | FTP | other *** search
/ PD ROM 1 / PD ROM Volume I - Macintosh Software from BMUG (1988).iso / Stacks / Updates⁄New / TEXAS for BMUG / C progs / TEXAS XFCNs ƒ / getPtrRecordXFCN.1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-12-08  |  3.0 KB  |  165 lines  |  [TEXT/KAHL]

  1. /* HyperCard XFCN to grab a ptr record and return with the value of the
  2.  * ptr itself (the offset into the text file) ...
  3.  *
  4.  * called as     getPtrRecord ( N, P)
  5.  *
  6.  * N = ptr record number of first line to display
  7.  * P = ptr file refNum
  8.  *
  9.  * The result is returned as a nice printable zero-terminated string,
  10.  * consisting simply of the (decimal) value of the ptr....
  11.  *
  12.  * If a mistake is detected, a null string is returned and the function
  13.  * also emits a beep....
  14.  *
  15.  * Function is stored as XFCN number 600, named "getPtrRecord"....
  16.  *
  17.  * 871208 ^z
  18.  */
  19.  
  20. #include <MacTypes.h>
  21. #include <FileMgr.h>
  22. #include <OSUtil.h>
  23. #include <HyperXCmd.h>
  24. #include <proto.h>
  25.  
  26.  
  27. pascal void main (XCmdBlockPtr paramPtr);
  28. long getThePtr (long ptrNum, int refNum);
  29. int putNum (char *ans, long num);
  30. void complain (XCmdBlockPtr paramPtr);
  31.  
  32.  
  33. pascal void main (paramPtr)
  34.   XCmdBlockPtr paramPtr;
  35.   {
  36.     int ptrFileNum;
  37.     long ptrNum, ptr;
  38.     int len;
  39.     Handle answer;
  40.     
  41.     if (paramPtr->paramCount != 2)
  42.       {
  43.           complain (paramPtr);
  44.           return;
  45.       }
  46.      
  47.     ptrNum = atol (*(paramPtr->params[0]));
  48.     ptrFileNum = atol (*(paramPtr->params[1]));
  49.     
  50.     if (ptrNum < 0 || ptrFileNum == 0)
  51.       {
  52.           complain (paramPtr);
  53.           return;
  54.       }
  55.     
  56.     answer = NewHandle (32);
  57.     ptr = getThePtr (ptrNum, ptrFileNum);
  58.     len = putNum (*answer, ptr);
  59.     *(*answer + len) = '\0';
  60.     paramPtr->returnValue = answer;
  61.     return;
  62.   }
  63.  
  64.  
  65. /* function to fetch the value of the nth ptr from file ptrFileNum ...
  66.  * return illegal value (-1) for result if something goes wrong....
  67.  */
  68.  
  69. long getThePtr (n, ptrFileNum)
  70.   long n;
  71.   int ptrFileNum;
  72.   {
  73.     long bytes = sizeof (long), result;
  74.     
  75.     if (SetFPos (ptrFileNum, fsFromStart, n * sizeof (long)) != noErr ||
  76.             FSRead (ptrFileNum, &bytes, &result) != noErr)
  77.         return (-1);
  78.  
  79.     return (result);
  80.   }
  81.  
  82.  
  83.  
  84. /* function to beep and set the return string to null (= "")
  85.  */
  86.  
  87. void complain (paramPtr)
  88.   XCmdBlockPtr paramPtr;
  89.   {
  90.     Handle answer;
  91.     
  92.       SysBeep (10);
  93.     answer = NewHandle (1);
  94.     **answer = '\0';
  95.     paramPtr->returnValue = answer;
  96.     return;
  97.   }
  98.  
  99.  
  100.  
  101. /* function to convert alphabetic string to a long integer ... from LSC
  102.  * library.... simplified to avoid using isspace() & isdigit() .... */
  103.  
  104. long atol (s)
  105.   register char *s;
  106.   {
  107.     register char signflag = 0;
  108.     register long r = 0;
  109.  
  110.     while ((*s == ' '))
  111.         s++;
  112.         
  113.     if (*s == '-')
  114.       {
  115.         signflag = 1;
  116.         s++;
  117.       }
  118.     else if (*s == '+')
  119.          s++;
  120.  
  121.     while (*s >= '0' && *s <= '9') 
  122.         r = r * 10 + (*s++ - '0');
  123.     
  124.     return (signflag ? -r : r);
  125. }
  126.  
  127.  
  128. /* function to convert a number into a string and put it into the chosen
  129.  * target place ... returns the number of characters stored ...
  130.  * based on K&R p. 60 example of itoa()....
  131.  */
  132.  
  133. int putNum (ans, num)
  134.   char *ans;
  135.   long num;
  136.   {
  137.     int i, j, s, result;
  138.     
  139.     i = 0;
  140.     s = 1;
  141.     if (num < 0)
  142.       {
  143.         num = -num;
  144.         s = -1;
  145.       }
  146.     
  147.     do
  148.           ans[i++] = num % 10 + '0';
  149.     while ((num /= 10) > 0);
  150.     
  151.     if (s < 0)
  152.         ans[i++] = '-';
  153.     result = i;
  154.     
  155.     for (--i, j = 0; j < i; ++j, --i)
  156.       {
  157.           s = ans[i];
  158.           ans[i] = ans[j];
  159.           ans[j] = s;
  160.       }
  161.  
  162.     return (result);
  163.   }
  164.   
  165.